from core import app, db
import modules
from modules.user import UserModel
from sqlalchemy import text

if __name__ == "__main__":
    try:
        with app.app_context():
            #db.session.execute(text("drop table if exists sys_image"))
            #db.session.commit()
            if app.config.get('RUN_MODE') == 'dev':
                db.drop_all()
            
            db.create_all()
            db.session.execute(text("update sys_collection set status = '4' where status='1'"))
            db.session.commit()

            if app.config.get('RUN_MODE') == 'dev':
                data = {
                    'account': 'admin',
                    'password': '123456',
                    'nickname': 'admin',
                    'phone': '13800000000',
                    'email': 'admin@example.com'
                }
                user = UserModel(
                    account = data['account'],
                    password = UserModel.md5_encrypt(data.get('password') if data.get('password') else '123456'),
                    nickname = data['nickname'],
                    phone = data['phone'],
                    email = data['email'],
                    status = '1'
                )
                db.session.add(user)
                db.session.commit()

            app.run(debug=True, port=6060)
    except Exception as e:
        print(e)
        exit(1) 